package com.miyu.common.service.book.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.miyu.common.dao.commonDAO.book.BookRuleSetDao;
import com.miyu.common.pojo.book.BookPagePJ;
import com.miyu.common.pojo.book.BookRuleSet;
import com.miyu.common.service.book.BookCommonService;
import com.miyu.common.utils.bookPage.BookPageJx;
import com.miyu.common.utils.commonUtils.CommonCache;
import com.miyu.common.utils.commonUtils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class BookCommonServiceImpl implements BookCommonService {

    @Resource
    private BookRuleSetDao brsd;
    /**
     * 公共Page解析类 以及后续处理办法
     * @param params
     * params key 缓存取值 如无则随机生成一个 返回数据后及移除缓存
     * 如携带key 则缓存数据以key取缓存数 以JSONObject 返回
     * cond 需要解析的顺序  数组形式 bookSearch,bookChapterList,chapterContext,bookType
     * url 解析连接地址
     * @return
     */
    @Override
    public JSONObject BookPageJX(BookPagePJ params) {
        String ruleId = params.getRuleId();
        BookRuleSet bookRuleSet = new BookRuleSet();
        //随机生成一个key
        String key = "";;
        //规则
        if(StringUtils.isNotBlank(ruleId)){
            //规则
            if(params.getRule().isEmpty()){
                bookRuleSet = brsd.getById(ruleId);
                params.setRule(JSON.parseObject(bookRuleSet.getRuleSet()));
            }
        }
        if (StringUtils.isNotBlank(params.getKey())){
            key = params.getKey();
            params.setKey(key);
        }else{
            key =  CommonUtils.getUUID();
            params.setKey(key);
        }
        //解析后是否要返回缓存数据
        BookPageCommonJX(params);
        //返回缓存数据同一
        Object bookEhcache = CommonCache.getComData("bookEhcache", key);
        if (params.isRemove()){
             CommonCache.removeByKey("bookEhcache", key);
        }
        JSONObject rs = new JSONObject();
        rs.put("data", bookEhcache);
        return rs;
    }
    /**
     * 解析入口
     * 解析判断是否可通过解析
     * @param param
     * @return
     */
    public JSONObject BookPageCommonJX (BookPagePJ param){
        return  BookPageJx.BookPageJX(param);
    }

    /**
     * 本规则id
     * 单规则查询
     *
     * @param ruleId 规则id
     * @return {@link BookRuleSet}
     */
    @Override
    public BookRuleSet getBookRuleById(String ruleId) {
        return brsd.findById(ruleId).orElse(null);
    }

    /**
     * 查询书细节
     *
     * @param ruleId 规则id
     * @param url    url
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject queryBookDetail(String ruleId, String url) {
        /*规则参数*/
        List<String>cond = new ArrayList<>();
        cond.add("bookDetail");
        BookPagePJ bop = new BookPagePJ();
        bop.setKey("bookDetail");
        bop.setCond(cond);
        bop.setRuleId(ruleId);
        bop.setUrl(url);
        return BookPageJX(bop);
    }

    /**
     * 查询书章
     *url 当前书籍明细url
     * @param ruleId 规则id
     * @param url    url
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject queryBookChapter(String ruleId, String url) {
        BookRuleSet bookRule = getBookRuleById(ruleId);
        JSONObject temp = new JSONObject();
        //当前书籍明细url
        temp.put("bookUrl",url);
        /*规则参数*/
        JSONObject bookRuleSet = JSONObject.parseObject(bookRule.getRuleSet());
        JSONObject bookChapter = bookRuleSet.getJSONObject("bookChapter");
        String bookChapterList = bookChapter.getString("bookChapterList");
        /*有明细界面经过跳转到章节页面再取值
        * 替换原有取章节列表*/
        if(bookChapterList.indexOf("#")>-1){
           String[] bookChapterListArr = bookChapterList.split("#");
            BookPagePJ bops = new BookPagePJ();
            bops.setRuleStr(bookChapterListArr[0]);
            bops.setKey("chapterUrl");
            bops.setUrl(url);
            JSONObject chUrl = BookPageJX(bops);
            url = chUrl.getJSONArray("data").getJSONObject(0).getString("chapterUrl");
            bookRuleSet.getJSONObject("bookChapter").put("bookChapterList",bookChapterListArr[1]);
        }

        //先获取章节列表url
        //再获取当前章节规则
        List<String>cond = new ArrayList<>();
        cond.add("bookChapter");
        BookPagePJ bop = new BookPagePJ();
        bop.setKey("bookChapter");
        bop.setRule(bookRuleSet);
        bop.setCond(cond);
        bop.setRuleId(ruleId);
        bop.setUrl(url);
        bop.setTemp(temp);
        return BookPageJX(bop);
    }

    /**
     * 查询书阅读
     *
     * @param param 阅读页面 需包含 ruleId url 阅读链接
     *  返回是否是第一章或最后一章 以及当前书籍详情页
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject queryBookReader(JSONObject param) {
        /*规则参数*/
        List<String>cond = new ArrayList<>();
        cond.add("bookReader");
        BookPagePJ bop = new BookPagePJ();
        bop.setKey("bookReader");
        bop.setCond(cond);
        bop.setRuleId(param.getString("ruleId"));
        bop.setUrl(param.getString("url"));
        bop.setParam(param);
        return BookPageJX(bop);
    }

    /**
     * 查询搜索书
     *
     * @JSONObject param 规则id
     *
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject querySearchBook(JSONObject param){
        String ruleId = param.getString("ruleId");
        BookRuleSet bookRuleSet = getBookRuleById(ruleId);
        String rootSearch = bookRuleSet.getRootSearch();
        String search = param.getString("search");
        String url =StrUtil.format(rootSearch,search);
        /*规则参数*/
        List<String>cond = new ArrayList<>();
        cond.add("bookSearch");
        BookPagePJ bop = new BookPagePJ();
        bop.setKey("bookSearch");
        bop.setCond(cond);
        bop.setRuleId(ruleId);
        bop.setUrl(url);
        return BookPageJX(bop);
    }

    /**
     * 书籍采集 不取阅读章节
     *
     * @param param 参数
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject collectBook(JSONObject param) {
        String ruleId = param.getString("ruleId");
        String url =param.getString("url");

        JSONObject bookDetail = queryBookDetail(ruleId, url);
        JSONObject bookChapter = queryBookChapter(ruleId, url);


        JSONObject rs = new JSONObject();
        rs.put("bookDetail",bookDetail.getJSONArray("data").getJSONObject(0).getJSONObject("bookDetail"));
        rs.put("bookChapter",bookChapter.getJSONArray("data").getJSONObject(0).getJSONArray("chapterList"));
    return  rs;
    }
}
